#include <OSA.h>
#include <1wire.h>
#include <uart.h> //FIXME deve sparire
#include <string.h>
#include <binsem.h>

#define ConvertT    	OW_reset_pulse();OW_write_byte (SKIP_ROM);OW_write_byte  (CONVERT_T)


extern OST_PMSG    pmsg;
extern OST_PMSG    p_tmsg;


void task_temp (void)
{
	unsigned char temp;
	static char welcome_message[] = "MSTDEVICE\r\n";
	unsigned char not_present[] = "SLAVE Device is not present\r\n";
	unsigned char dev_present [] = "SLAVE Device is present\r\n";
	unsigned char ser_num [] = "SERIAL NUMBER:  \r\n";
	int i;
   
	char BUF[10];               // Buffer for received data from UART
    static char Message[11];    // Message body
 
    OS_CreateMsg(pmsg);         // First we create message
	OS_CreateMsg(p_tmsg);		// messaggio temperatura
	
   float temp_celsius;
   unsigned char serial_number[8];
   unsigned char str[16];
   float temperature;
	for (;;)
    {
	    
	    OS_WaitBSem(BS_GO_READ_T);
        memcpy(Message, welcome_message,10);
	    OS_SendMsg(pmsg, (OST_MSG)Message);
	    Detect_Slave_Device();
   
  	    OW_write_byte (SKIP_ROM);            // Send a command to read a serial number
        OW_write_byte  (CONVERT_T);
   
   for(i=0;i<1000;i++)
           Delay_Us(1200);


		temperature =  Read_Temperature_DS18S20();
		OS_SendMsg(p_tmsg, (OST_MSG)&temperature);     	
        OS_ResetBSem(BS_GO_READ_T);
        OS_Yield();
    }
}

